package com.example.frickytest;

import java.util.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.content.Intent;
import android.widget.Toast;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.view.MotionEvent;


public class MainActivity extends Activity {

	protected int my_requestCode = 1550;
	private Spinner spinner_edit_height;
	private RadioButton radiobutton_Man, radiobutton_Woman;
	private ArrayAdapter<String> adapter;
	Animation myAnimation;
	private static final String[] countriesStr ={ " 170", " 175", " 180", " 185" };
	private List <String> list_H; 
	private String tempNayou;
	private String addString;
	private Button button_ad;
	private Button button_de;
	private Button ok;
	private EditText editText;

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        radiobutton_Man = (RadioButton) findViewById(R.id.Sex_Man);
        radiobutton_Woman = (RadioButton) findViewById(R.id.Sex_Woman);
        spinner_edit_height = (Spinner) findViewById(R.id.spinner_height_Edit);
        list_H = new ArrayList<String>();
        
        myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_H );
        adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
        spinner_edit_height.setAdapter(adapter);
        
        button_ad = (Button) findViewById(R.id.Button_ad);
        button_de = (Button) findViewById(R.id.Button_de);
        ok = (Button) findViewById(R.id.button_OK);
        editText = (EditText) findViewById(R.id.editText1);
        
        ok.setEnabled(false);
        button_de.setEnabled(false);
        
		button_ad.setOnClickListener(new Button.OnClickListener() {
			
			public void onClick(View v) {

				addString = editText.getText().toString();
				
				if (addString.equals("")) {
					
					Toast.makeText(MainActivity.this, "请输入数值！",
							Toast.LENGTH_SHORT).show();
					return;
				}
				
			    Boolean flag = check(adapter, addString);

				if (flag) {
					adapter.add(addString);

					int post = adapter.getPosition(addString);

					spinner_edit_height.setSelection(post);

					editText.setText("");
					
					Toast.makeText(MainActivity.this, "添加"+addString, Toast.LENGTH_SHORT)
					.show();
					
			        ok.setEnabled(true);
			        button_de.setEnabled(true);
				}
			}

			public Boolean check(ArrayAdapter<String> adapter, String addString) {
				Boolean checkflag = true;

				for (int i = 0; i < adapter.getCount(); i++) {
					if (addString.equals(adapter.getItem(i))) {
						Toast.makeText(MainActivity.this, "输入的数值重复！",
								Toast.LENGTH_SHORT).show();
						checkflag = false;
						break;
					} 
				}
				return checkflag;
			}
		});
		button_de.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View v) {
				
				addString = editText.getText().toString();
				
				if (addString.equals("")) {
					
					Toast.makeText(MainActivity.this, "请输入数值！",
							Toast.LENGTH_SHORT).show();
					return;
				}
				
				Boolean flag = check(adapter, addString);
				
				if (flag) {
					if (spinner_edit_height.getSelectedItem() != null) {

						adapter.remove(editText.getText().toString());

						editText.setText("");
						
						spinner_edit_height.setSelection(0);
						if (adapter.getCount() == 0) {

							Toast.makeText(MainActivity.this, "删完了",
									Toast.LENGTH_SHORT).show();
							
					        ok.setEnabled(false);
					        button_de.setEnabled(false);
						}
					}
				}
			}
			
			public Boolean check(ArrayAdapter<String> adapter, String addString) {
				Boolean checkflag = false;
				
				for (int i = 0; i < adapter.getCount(); i++) {

					if (addString.equals(adapter.getItem(i))) {
						checkflag = true;
						break;
					} 
				}
				if (!checkflag) {
					Toast.makeText(MainActivity.this, "输入的数值不存在！",
							Toast.LENGTH_SHORT).show();
				}
				return checkflag;
			}
		});        
        
/*
 * String start     
 */
		spinner_edit_height
				.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						
						tempNayou = countriesStr[arg2];
						//arg0.setVisibility(View.VISIBLE);
					}

					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				});
		
		spinner_edit_height.setOnTouchListener(new Spinner.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				
				v.startAnimation(myAnimation);

				//v.setVisibility(View.INVISIBLE);
				return false;
			}
		});
		
		spinner_edit_height
				.setOnFocusChangeListener(new Spinner.OnFocusChangeListener() {
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
					}
				});
/*
 * String end     
 */
        ok.setOnClickListener(new Button.OnClickListener() {
        	
        	public void onClick(View v) {
        		try {            		
            		double height = Double.parseDouble(tempNayou);

            		String sex = "";
            		RadioButton rb1 = (RadioButton) findViewById(R.id.Sex_Man);
            		if (rb1.isChecked()) {
            		sex = "M";
            		} else {
            			sex = "F";
            		}

            		Intent intent = new Intent();
            		intent.setClass(MainActivity.this, BMIActivity.class);

            		Bundle bundle = new Bundle();
            		bundle.putDouble("height", height);
            		bundle.putString("sex", sex);

            		intent.putExtras(bundle);

            		//startActivity(intent);
            		startActivityForResult(intent, my_requestCode);
        		} catch (Exception e){
        			Toast.makeText(MainActivity.this,
        					R.string.errorString, Toast.LENGTH_LONG).show();
        		}	
        	}
       	
        });
        
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v) {
        		MainActivity.this.finish();
        	}
        });
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_OK:

			Bundle bunde = data.getExtras();
			String sex = bunde.getString("sex");
			//double height = bunde.getDouble("height");
			//edit_height.setText("" + height);
			if (sex.equals("M")) {
				radiobutton_Man.setChecked(true);
			} else {
				radiobutton_Woman.setChecked(true);
			}
			break;
		default:
			break;
		}
	}
}
